<?php

namespace zhtfrontend\controllers;

use common\components\extenders\AjaxObject;
use common\components\helpers\Fun;
use zhtfrontend\models\datas\PtaActive;
use yii\web\Response;
use yii\helpers\Url;
use yii\web\BadRequestHttpException;
use common\models\datas\Customer;
use yii\helpers\ArrayHelper;
use doumiaoduo\buildform\BuildForm;
use yii\data\Pagination;
use yii\helpers\Json;

class ActiveController extends \zhtfrontend\components\bases\BaseController
{
    public function actionIndex()
    {
        $param = \Yii::$app->request->get();
        $pageSize = isset($param['pageSize']) ? $param['pageSize'] : 12;
        $data = PtaActive::find()->orderBy('id ASC');
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => $pageSize]);
        isset($param['page']) ? $pages->page = $param['page'] - 1 : 0;
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        $array = Fun::asArray($model, ['zhtfrontend_operate']);

        return $this->render('index', ['data' => $array, 'pages' => $pages]);
    }


    public function actionCreate()
    {
        $this->layout = false;
        $model = new PtaActive();
        $model->site_id = Fun::getSiteId();
        $model->user_id = 1;
        $info = Customer::find()->all();
        $form = new BuildForm('@app/components/forms/activeForm.php');
        $form->loadModel(['active' => $model]);
//        $form->setItems('active', 'customer_id', ArrayHelper::map($info, 'id', 'name'));
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('active');
            $model->begin_time = \Yii::$app->formatter->asTimestamp($model->begin_time);
            $model->end_time = \Yii::$app->formatter->asTimestamp($model->end_time);
            $model->created_at = \Yii::$app->formatter->asTimestamp($model->created_at);
            if ($model->save(false)) {
                return $this->redirectMessage('添加成功', Fun::url(['active/index']), true);
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }

    public function actionUpdate()
    {
        $this->layout = false;
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = PtaActive::findOne(\Yii::$app->request->get('id'));
        if ($model === null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $customer_name = Customer::find()->all();
        $model->begin_time = \Yii::$app->formatter->asDatetime($model->begin_time);
        $model->end_time = \Yii::$app->formatter->asDatetime($model->end_time);
        $model->created_at = \Yii::$app->formatter->asDatetime($model->created_at);
        $form = new BuildForm('@app/components/forms/activeForm.php');
        $form->loadModel(['active' => $model]);
        if ($form->loadData(\Yii::$app->request->post()) && $form->validate()) {
            $model = $form->getModel('active');
            $model->begin_time = \Yii::$app->formatter->asTimestamp($model->begin_time);
            $model->end_time = \Yii::$app->formatter->asTimestamp($model->end_time);
            $model->created_at = \Yii::$app->formatter->asTimestamp($model->created_at);
            if ($model->save(false)) {
                return $this->redirectMessage(\Yii::t('zhtfrontend', '信息修改成功'), Url::to('active/index'), 'success');
            }
        }
        return $this->render('create', ['form' => $form->render()]);
    }


    public function actionShow()
    {
        $this->layout = false;
        if (\Yii::$app->request->get('id') == null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model = PtaActive::findOne(\Yii::$app->request->get('id'));
        if ($model === null) {
            throw new BadRequestHttpException(\Yii::t('zhtfrontend', '信息不存在'), 404);
        }
        $model->begin_time = \Yii::$app->formatter->asDatetime($model->begin_time);
        $model->end_time = \Yii::$app->formatter->asDatetime($model->end_time);
        $model->created_at = \Yii::$app->formatter->asDatetime($model->created_at);
        $form = new BuildForm('@app/components/forms/activeForm.php');
        $form->loadModel(['active' => $model]);
        $form->setOptions('active', 'customer_id', ['disabled' => 'disabled']);
        $form->setOptions('active', 'title', ['disabled' => 'disabled']);
        $form->setOptions('active', 'content', ['disabled' => 'disabled']);
        $form->setOptions('active', 'begin_time', ['disabled' => 'disabled']);
        $form->setOptions('active', 'end_time', ['disabled' => 'disabled']);
        $form->setOptions('active', 'created_at', ['disabled' => 'disabled']);
        $form->setOptions('active', 'states', ['disabled' => 'disabled']);
        /*if( $form->loadData(\Yii::$app->request->post()) && $form->validate() ){
            if($model->save(false)){
                return $this->redirectMessage(\Yii::t('zhtfrontend','信息修改成功'),Url::to('active/index'),'success');
            }
        }*/
        return $this->render('create', ['form' => $form->render()]);
    }

}
